package thread;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class Test extends JFrame{
    private JTextArea[] ta=new JTextArea[3];
    public Test()  {
        setTitle("售票");
        setSize(1000,500);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLayout(null);//设置一个空的窗口
        int x=40;
        for (int i=0;i<3;i++){
            ta[i]=new JTextArea();
            ta[i].setEditable(false);//禁止编辑
            JScrollPane sp=new JScrollPane(ta[i]);
            sp.setBounds(x,10,300,300);
            add(sp);
            x+=310;
        }
        JButton btn=new JButton("开始");
        btn.setBounds(400,350,200,25);
        add(btn);
        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (int i=0;i<3;i++){
                    ta[i].setText("");
                }
                List<Ticket> lists=new ArrayList<>();
                for (int i=0;i<10;i++){
                    lists.add(new Ticket(i+1));
                }
                SallTicket st1=new  SallTicket(lists,ta[0]);
                SallTicket st2=new  SallTicket(lists,ta[1]);
                SallTicket st3=new  SallTicket(lists,ta[2]);
                st1.start();st2.start();st3.start();
                while (true){
                    if (!st1.isAlive()&&!st2.isAlive()&&st3.isAlive())break;
                    System.out.println(st1.getState());
                    System.out.println(st2.getState());
                    System.out.println(st3.getState());

                }
            }
        });
    }

    public static void main(String[] args){
        /*SallTicket st1=new SallTicket("张三",20);
        SallTicket st1=new SallTicket("张三",20);
       SallTicket st2=new SallTicket("李四",10);
        SallTicket st3=new SallTicket("张而",30);
        Thread th1=new Thread(st1,"tom");
        Thread th2=new Thread(st1,"tot");
        Thread th3=new Thread(st1,"too");
        th1.start();th2.start();th3.start();*/

        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Test();
            }
        });
    }
}
